tg-me.com/python_codes/150
Last Update:
Checks if a string is an anagram of another string (case-insensitive, ignores spaces, punctuation and special characters).
Use isalnum() to filter out non-alphanumeric characters, lower() to transform each character to lowercase.
Use collections.Counter to count the resulting characters for each string and compare the results.
Code:
from collections import Counter
def is_anagram(s1, s2):
return Counter(
c.lower() for c in s1 if c.isalnum()
) == Counter(
c.lower() for c in s2 if c.isalnum()
)
EXAMPLE
is_anagram("#anagram", "Nag a ram!")
Output:
True
Share and Support
@Python_Codes
BY Python Codes
Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283
Share with your friend now:
tg-me.com/python_codes/150